<?php

namespace Modules\Admin\Models;
use Modules\Admin\Models\BaseModel;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Modules\Admin\Services\UserService;
class User extends Authenticatable implements JWTSubject
{
	use Notifiable;
    protected $fillable = [
        'phone','nick_name','password','image_id','status'
    ];
	protected $hidden = ['password'];
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    public function getJWTCustomClaims()
    {
        return [];
    }
	/**
     * @name  关联图片
     */
    public function image_one()
    {
        return $this->hasOne('Modules\Admin\Models\Image','id','image_id');
    }
	private function gender_list(){
		return [
			'0'=>'未知',
			'1'=>'男',
			'2'=>'女',
		];
	}
	/**
     * @name  列表数据处理
     */
	public function data_image($data){ 
		$gender = $this->gender_list();
		foreach($data as $k=>$v){
			$data[$k]['gender'] = $gender[$v['gender']];
			if(isset($v['image_one']['status']) && $v['image_one']['status'] == 1){
				$data[$k]['image'] = $v['image_one']['url'];
			}else{
				$data[$k]['image'] = '';
			}
		}
		return $data;
	}
	/**
	 * @name  状态公共方法
	 */
	protected static function common_status(){
		return BaseModel::common_status();
	}
	/**
	 * @name  调整状态
	 */
	public function blog_status_update($data,$id,$url,$msg = '',$error = ''){
		if($msg == ''){
			$msg = config('admin.status_success');
		}
		if($error == ''){
			$error = config('admin.status_error');
		}
		unset($data['_method']);
        if ($this->where('id',$id)->update($data) !== false){
            return response()->json([
				'status'=>1, 
				'error' => '',
				'msg' => $msg,
				'url'=>$url
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => $error,
				'msg' => '',
				'url'=>$url 
			],201);
		}
	}
	/**
	 * @name  删除
	 */
	public function blog_user_destroy($ids,$url){
        if ($this->destroy($ids)){
            return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => config('admin.delete_success'),
				'url'=>$url
			],200);
        }else{
			return response()->json([
				'status'=>0,
				'error' => config('admin.delete_error'),
				'msg' => '',
				'url'=>$url 
			],201);
		}
	}
	/**
	 * @name  用户登录
	 */
	 public function login($data){
		 $user_arr = [];
		 if($data['type'] == 1){
			 $id = $this->where(['phone'=>$data['phone']])->value('id');
			 if(!$id){
				$user_arr = [
						'phone'=>$data['phone']
				    ];
			 }
		 }else{
			$user_info = (new UserService())->get_user_info($data); 
			if(isset($user_info['code'])){
				return $user_info;
			}
			if(isset($user_info['union_id'])){
				$id = $this->where(['union_id'=>$user_info['union_id']])->value('id');
				if($id){
					return $id;
				}
			}
			if($data['provider'] == 1){
			   $id = $this->where(['xcx_open_id'=>$user_info['openId']])->value('id');
			   if(!$id){
				   $user_arr = [
						'xcx_open_id'=>$user_info['openId'],
						'nick_name'=>$user_info['nickName'],
						'avatar_url'=>$user_info['avatarUrl'],
						'gender'=>$user_info['gender']
				   ];
			   }
			}else if($data['provider'] == 4){
			    $id = $this->where(['gzh_open_id'=>$user_info['gzh_open_id']])->value('id');
			    if(!$id){
				   $user_arr = [
						'gzh_open_id'=>$user_info['gzh_open_id'],
						'nick_name'=>$user_info['nick_name'],
						'avatar_url'=>$user_info['avatar_url'],
						'gender'=>$user_info['gender']
				   ];
			    }
			}else if($data['provider'] == 3){
				$id = $this->where(['gzh_open_id'=>$user_info['gzh_open_id']])->value('id');
			    if(!$id){
				   $user_arr = [
						'gzh_open_id'=>$user_info['gzh_open_id'],
						'nick_name'=>$user_info['nick_name'],
						'avatar_url'=>$user_info['avatar_url'],
						'gender'=>$user_info['gender']
				   ];
			    }
			}	
		 }
		 if(!$id){
			 if(isset($user_info['union_id'])){
				 $user_arr['union_id'] = $user_info['union_id'];
			 }
			 $user_arr['password'] =  bcrypt(config('admin.user_password'));
			 $user_arr['created_at'] =  date('Y-m-d H:i:s');
			 $id = $this->insertGetId($user_arr);
		 }else{
			 if($this->where(['id'=>$id])->value('status') == 0){
				return ['msg'=>'用户被禁用！'];
			 } 
		 }
		 if($id){
			 return $id;
		 }else{
			 return ['msg'=>'登录失败！'];
		 }
	 }
	
}
